//
//  CommunityCardShareCell.m
//  QRMaster
//
//  Created by lc on 16/6/1.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "CommunityCardShareCell.h"
#import "ManagerTodayShareCardInfoVC.h"
@interface CommunityCardShareCell()<UIGestureRecognizerDelegate,UIScrollViewDelegate>
{
    CGFloat lastOffset;
    BOOL flag;
}
@end

@implementation CommunityCardShareCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addAllSubView];
    }
    return self;
}

- (void)addAllSubView{
    
    _showView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 56)];
    _showView.contentSize = CGSizeMake(UI_SCREEN_WIDTH + 100, 0);
    _showView.bounces = NO;
    _showView.showsHorizontalScrollIndicator = NO;
    _showView.userInteractionEnabled = YES;
    _showView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_showView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapThing)];
    [self.showView addGestureRecognizer:tap];
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 7, 60, 18)];
    _nameLabel.font = [UIFont systemFontOfSize:16];
    _nameLabel.textColor = UIColorFromRGB(0x333333);
    [_showView addSubview:_nameLabel];
    
    _shareImg = [[UIImageView alloc] initWithFrame:CGRectMake(_nameLabel.frame.origin.x+_nameLabel.frame.size.width+10, 7, 19, 16)];
    _shareImg.image = [UIImage imageNamed:@"sharefor_grey"];
    [_showView addSubview:_shareImg];
    
    _mobileLabel = [[UILabel alloc] initWithFrame:CGRectMake(_shareImg.frame.origin.x+19+10, 7, 150, 16)];
    _mobileLabel.textColor = UIColorFromRGB(0x333333);
    _mobileLabel.font = [UIFont systemFontOfSize:14];
    [_showView addSubview:_mobileLabel];
    
    _shareTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 33, 200, 14)];
    _shareTimeLabel.textColor = UIColorFromRGB(0x727272);
    _shareTimeLabel.font = [UIFont systemFontOfSize:12];
    [_showView addSubview:_shareTimeLabel];
    
    _statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-16-40, 14.5, 40, 27)];
    _statusLabel.font = [UIFont systemFontOfSize:14];
    _statusLabel.backgroundColor = UIColorFromRGB(0x7dc234);
    _statusLabel.textAlignment = NSTextAlignmentCenter;
    _statusLabel.textColor = [UIColor whiteColor];
    _statusLabel.text = @"正常";
    [_showView addSubview:_statusLabel];
    
    _effectiveBtn = [[UIButton alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH, 0, 50, 56)];
    _effectiveBtn.backgroundColor = UIColorFromRGB(0x727272);
    _effectiveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_effectiveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_effectiveBtn addTarget:self action:@selector(effectiveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_showView addSubview:_effectiveBtn];
    
    _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH+50, 0, 50, 56)];
    _deleteBtn.backgroundColor = [UIColor redColor];
    _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_showView addSubview:_deleteBtn];
    
}

- (void)setCellWithModel:(ComTodayAndAllShareCardModel *)model{
    _nameLabel.text = model.shareCardUserName;
    [_nameLabel sizeToFit];
    _shareImg.frame = CGRectMake(_nameLabel.frame.origin.x + _nameLabel.frame.size.width+10, 7, 19, 16);
    _mobileLabel.frame = CGRectMake(_shareImg.frame.origin.x + 19 + 10, 7, 150, 16);
    _mobileLabel.text = [NSString stringWithFormat:@"+%@ %@",model.shareAreaCode,model.telePhone];
    _shareTimeLabel.text = [NSString stringWithFormat:@"分享时间: %@",model.shareCardTime];
    if(model.enable==1){
        _statusLabel.text = @"正常";
        _statusLabel.backgroundColor = UIColorFromRGB(0x7dc234);
        [_effectiveBtn setTitle:@"失效" forState:UIControlStateNormal];
        [_effectiveBtn setBackgroundColor:UIColorFromRGB(0x727272)];
        
    }else{
        _statusLabel.text = @"失效";
        _statusLabel.backgroundColor = UIColorFromRGB(0x727272);
        [_effectiveBtn setTitle:@"启用" forState:UIControlStateNormal];
        [_effectiveBtn setBackgroundColor:UIColorFromRGB(0x7dc234)];
    }
    
}


#pragma mark -- roomNo自适应长度
-(CGFloat)calculateWidth:(NSString *)str
{
    CGSize size =CGSizeMake(_nameLabel.frame.size.width,18);
    
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName,nil];
    
    //ios7方法，获取文本需要的size，限制宽度
    
    CGSize  actualsize =[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    return actualsize.width;
}

#pragma mark cell return origin state

- (void)prepareForReuse{
    
    [super prepareForReuse];
    _showView.contentOffset = CGPointMake(0, 0);
}

- (void)effectiveBtnClick:(UIButton *)sender{
    if (self.effectiveBlock) {
        self.effectiveBlock(sender.tag);
    }
}

- (void)deleteBtnClick:(UIButton *)sender{
    if (self.deleteBlock) {
        self.deleteBlock(sender.tag);
    }
}

#pragma mark scrollview delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    //首先发送一个通知，去查看其他的cell有没有移动，如果有，恢复
     NSDictionary *dic = @{@"cellindex":[NSString stringWithFormat:@"%li",(long)_showView.tag]};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"cellnot" object:nil userInfo:dic];

    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //判断
    CGFloat offsetX = _showView.contentOffset.x;
    if (offsetX > lastOffset + 1) {
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [_showView setContentOffset:CGPointMake(120, 0)];
        } completion:nil];
        
    }else if (offsetX < lastOffset){
        
        [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [_showView setContentOffset:CGPointMake(0, 0)];
        } completion:nil];
        
    }
    lastOffset = offsetX;
}
#pragma mark  tag method

- (void)tapThing{
    
    NSLog(@"========================%li",(long)_showView.tag);
    if (_isTodayShareCard) {
        NSDictionary *dic = @{@"cellindex":[NSString stringWithFormat:@"%li",(long)_showView.tag]};
        [[NSNotificationCenter defaultCenter]postNotificationName:@"cellnot" object:@"CommunityTodayShareCard" userInfo:dic];
    }else{
        NSDictionary *dic = @{@"cellindex":[NSString stringWithFormat:@"%li",(long)_showView.tag]};
        [[NSNotificationCenter defaultCenter]postNotificationName:@"cellnot" object:@"CommunityAllShareCard" userInfo:dic];
    }
}

#pragma mark gesture delegate   aim  mutable gesture can coexist
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
